﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  UtilityCommon   
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  : 
 * Created Time :  2013/5/22 0:24:49
 * Update Man   :  Joey Liang
 * Update Time  :  2013/7/22 0:24:49
************************************************************************/


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JoeyLibs.Utility
{
    public class UtilityCommon
    {
        /// <summary>
        /// 进行遍历
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="array">遍历的集合</param>
        /// <param name="action">遍历到每一项执行的方法</param>
        /// <param name="breakBeforeFunc">跳出循环的方法(action执行前)</param>
        /// <param name="breakAfterFunc">跳出循环的方法(action执行后)</param>
        public static void ForeachHandle<T>(IEnumerable<T> array, Action<T> action,
            Func<T, bool> breakBeforeFunc, Func<T, bool> breakAfterFunc)
        {
            foreach (T item in array)
            {
                if (breakBeforeFunc != null && breakBeforeFunc(item))
                {
                    break;
                }

                action(item);
                if (breakAfterFunc != null && breakAfterFunc(item))
                {
                    break;
                }
            }
        }

        /// <summary>
        /// 进行遍历
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="array">遍历的集合</param>
        /// <param name="action">遍历到每一项执行的方法</param>
        public static void ForeachHandle<T>(IEnumerable<T> array, Action<T> action)
        {
            ForeachHandle<T>(array, action, null, null);
        }

        /// <summary>
        /// 进行遍历
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="array">遍历的集合</param>
        /// <param name="action">遍历到每一项执行的方法</param>
        /// <param name="breakBeforeFunc">跳出循环的方法(action执行前)</param>
        /// <param name="breakAfterFunc">跳出循环的方法(action执行后)</param>
        public static void ForeachHandle<T>(IEnumerable array, Action<T> action, 
            Func<T, bool> breakBeforeFunc, Func<T, bool> breakAfterFunc)
        {
            foreach (var item in array)
            {
                if (item is T)
                {
                    T t = (T)item;
                    if (breakBeforeFunc != null && breakBeforeFunc(t))
                    {
                        break;
                    }

                    action(t);
                    if (breakAfterFunc != null && breakAfterFunc(t))
                    {
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 进行遍历
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="array">遍历的集合</param>
        /// <param name="action">遍历到每一项执行的方法</param>
        public static void ForeachHandle<T>(IEnumerable array, Action<T> action)
        {
            ForeachHandle<T>(array, action, null, null);
        }   
    }
}
